package jdbc;

import java.sql.*;
import java.text.MessageFormat;

/**
 * @author oldliu
 * @since 1.0
 */
public class JdbcDemo1 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url = "jdbc:mysql://localhost:3306/java76?useSSL=false";
        Connection connection = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = DriverManager.getConnection(url, "root", "root1234");
            System.out.println(connection.getClass().getName());
            System.out.println("链接已经建立");
            st = connection.createStatement();
            System.out.println("上下文:" + st.getClass().getName());
            rs = st.executeQuery("select * from s_subject");
            System.out.println(rs.getClass().getName());
            while (rs.next()) {
                long id = rs.getLong("id");
                String name = rs.getString("subjectname");
                int hour = rs.getInt("classhour");
                long gid = rs.getLong("gradeid");
                System.out.println(MessageFormat.format("课程信息：id={0} ,名称={1},{2},{3}\n", id, name, hour, gid));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                st.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
